package eai.tp1.core.datamodel;

/**
 * This class converts the data that comes from the website into a correct date to save to
 * the xml file.
 * 
 * @author Simão
 */
public class Date {

    private String[] monthTags = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "out", "nov", "dec"};
    private int day;
    private int month;
    private int year;

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public Date(String date) {

        String[] data = date.split("-");
        this.day = Integer.parseInt(data[0]);

        this.month = getMonth(data[1]);

        int yearTemp = Integer.parseInt(data[2]);

        if (yearTemp == 0) {
            this.year = 2000;
        } else if (yearTemp > 0 && yearTemp < 10) {
            this.year = 2000 + yearTemp;
        } else {
            this.year = 1900 + yearTemp;
        }

    }

    public String getDay() {

        if (day < 10 && day > 0) {
            return "0" + day;
        } else {
            return "" + day;
        }
    }

    public void setDay(int day) {
        this.day = day;
    }

    public String getMonth() {
        
        if (month < 10 && month > 0) {
            return "0" + month;
        } else {
            return "" + month;
        }
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    private int getMonth(String month) {

        for (int i = 0; i < monthTags.length; i++) {
            if (month.toLowerCase().equals(monthTags[i])) {
                return i + 1;
            }
        }

        return 0;
    }

    @Override
    public String toString() {
        return day + "-" + month + "-" + year;
    }
}
